<?php

namespace app\command;

use app\admin\model\advertiser\AdvertiserThird;
use app\admin\model\base\SysConfig;
use app\admin\model\iot\ScanBagWay;
use app\admin\model\iot\ScanBagWayDevice;
use JetBrains\PhpStorm\NoReturn;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 系统数据同步
 */
class SystemSync extends Command
{
    protected function configure()
    {
        $this->setName('SystemSync')->setDescription('系统数据同步');
    }

    #[NoReturn] protected function execute(Input $input, Output $output)
    {
        //加载系统配置
        SysConfig::loadConfig();
        //系统配置同步
        $this->sysConfigSyn();
        //第三方平台枚举同步
        $this->advertiserThirdSyn();
        //取袋路径配置同步
        $this->scanBagWaySyn();
        var_dump('同步完成');
        die;
    }

    /**
     * 系统配置同步
     * @return void
     */
    private function sysConfigSyn()
    {
        $config = SysConfig::$sysConfig;
        $keys = [];
        foreach ($config as $v) {
            $keys[] = $v['key'];
            //查询数据库是否有记录
            $Model = SysConfig::where(['key' => $v['key']])->find();
            if ($Model) {
                //有则判断更新value值
                $temp_value = array_merge($v['value'], json_decode($Model->value, true));
                foreach ($temp_value as $kk => $vv) {
                    if (!array_key_exists($kk, $v['value'])) {
                        unset($temp_value[$kk]);
                    }
                }
                //更新数据库
                $temp['value'] = json_encode($temp_value, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
                $temp['update_time'] = time();
                $Model->save($temp);
            } else {
                //没有则写入默认值
                $Model = new SysConfig();
                $v['value'] = json_encode($v['value'], JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
                $v['create_time'] = time();
                $v['update_time'] = time();
                $Model->save($v);
            }
        }
        //删除不属于keys的值
        $where[] = ['key', 'not in', $keys];
        SysConfig::where($where)->delete();
        //清除缓存
        SysConfig::cleanUpConfig();
        var_dump('系统配置同步成功');
        return;
    }

    /**
     * 第三方平台枚举同步
     * @return void
     */
    private function advertiserThirdSyn()
    {
        $PlatformArr = AdvertiserThird::$ThirdPlatformArr;
        $ids = [];
        foreach ($PlatformArr as $v) {
            $ids[] = $v['id'];
            //查询数据库是否有记录
            $Model = AdvertiserThird::where(['id' => $v['id']])->find();
            if ($Model) {
                //有则更新默认值
                $temp['type'] = $v['type'];
                $temp['name'] = $v['name'];
                $temp['update_time'] = time();
                $Model->save($temp);
            } else {
                //没有则写入默认值
                $Model = new AdvertiserThird();
                $v['prohibit'] = json_encode($v['prohibit']);
                $v['create_time'] = time();
                $v['update_time'] = time();
                $Model->save($v);
            }
        }
        //删除不属于keys的值
        $where[] = ['id', 'not in', $ids];
        AdvertiserThird::where($where)->delete();
        var_dump('第三方平台枚举同步成功');
        return;
    }

    /**
     * 取袋路径配置同步
     * @return void
     */
    private function scanBagWaySyn()
    {
        $WayArr = ScanBagWay::$ScanBagWayArr;
        $keys = [];
        foreach ($WayArr as $v) {
            $keys[] = $v['key'];
            //查询数据库是否有记录
            $Model = ScanBagWay::where(['key' => $v['key']])->find();
            if ($Model) {
                //有则更新默认值
                $temp['name'] = $v['name'];
                $temp['update_time'] = time();
                $Model->save($temp);
            } else {
                //没有则写入默认值
                $Model = new ScanBagWay();
                $v['update_time'] = time();
                $Model->save($v);
            }
        }
        //删除不属于keys的路径
        $where[] = ['key', 'not in', $keys];
        ScanBagWay::where($where)->delete();
        //删除不属于keys的设备
        ScanBagWayDevice::where($where)->delete();
        var_dump('取袋路径配置同步成功');
        return;
    }

}